package cn.acheng1314.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

object DateUtil {

    const val DATE_FORMATTER_yyyyMMdd = "yyyyMMdd"
    const val DATE_FORMATTER_yyyyMMdd_HHmmss = "yyyy-MM-dd HH:mm:ss"
    const val DATE_FORMATTER_yyyyMMddHHmmss = "yyyyMMddHHmmss"
    const val DATE_FORMATTER_SMS_MMddHHmm = "MM月dd日HH时mm分"


    /**
     * 求出日期差的日期集合
     *
     * @param startDate
     * @param endDate
     * @return
     */
    fun calDate(startDate: Date, endDate: Date?): List<Date>? {
        var startDate = startDate
        val result: MutableList<Date> = ArrayList()
        val start = Calendar.getInstance(Locale.CHINA)
        start.time = startDate
        while (startDate.before(endDate)) {
            result.add(start.time)
            //加一天
            start.add(Calendar.DAY_OF_MONTH, 1)
            startDate = start.time
        }
        return result
    }

    /**
     * 将字符串转为时间
     *
     * @param dateStr   时间格式的字符串
     * @param formatter 对应的时间格式化格式
     * @return
     * @eg dateStr = 20190901  formatter = yyyMMdd  return Date()
     */
    fun toDate(dateStr: String?, formatter: String?): Date? {
        val sf = SimpleDateFormat(formatter)
        try {
            return sf.parse(dateStr)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return null
    }

    /**
     * 格式化时间为对应格式的字符串
     *
     * @param date      时间
     * @param formatter 格式
     * @return
     */
    fun format(date: Date?, formatter: String?): String? {
        val sf = SimpleDateFormat(formatter)
        return sf.format(date)
    }

    fun getStartDateTime(): Date? {
        val instance = Calendar.getInstance(Locale.CHINA)
        instance[Calendar.HOUR_OF_DAY] = 0
        instance[Calendar.MINUTE] = 0
        instance[Calendar.SECOND] = 0
        return instance.time
    }

    /**
     * 获取昨日的起始时间
     *
     * @return
     */
    fun getYesterdayStartDateTime(): Date? {
        val instance = Calendar.getInstance(Locale.CHINA)
        instance.add(Calendar.DAY_OF_MONTH, -1)
        instance[Calendar.HOUR_OF_DAY] = 0
        instance[Calendar.MINUTE] = 0
        instance[Calendar.SECOND] = 0
        return instance.time
    }

    fun getNow(): Date? {
        return Calendar.getInstance(Locale.CHINA).time
    }

    /***
     * 获取几天前的日期格式
     *
     * @param dayNum
     * @return
     */
    fun getDateByDayNum(dayNum: Int): String? {
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        val cal = Calendar.getInstance()
        cal.time = Date()
        cal.add(Calendar.DAY_OF_MONTH, -dayNum)
        return sdf.format(cal.time)
    }

    /**
     * 计算 day 天后的时间
     *
     * @param date
     * @param day
     * @return
     */
    fun addDay(date: Date?, day: Int): Date? {
        val calendar = Calendar.getInstance()
        calendar.time = date
        calendar.add(Calendar.DAY_OF_MONTH, day)
        return calendar.time
    }

    fun transDate(dateStr: String?) = SimpleDateFormat(DATE_FORMATTER_yyyyMMdd_HHmmss).parse(dateStr)

}